package com.wjggy.core.action.posts;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.wjggy.core.action.view.PostsView;
import com.wjggy.core.action.view.UserView;
import com.wjggy.core.common.springview.ViewUtil;
import com.wjggy.core.entities.Category;
import com.wjggy.core.service.posts.CategoryService;


 /**
 *  <p>Description:  板块操作action</p>
 *  <p>Copyright:Copyright (c) 2012  </p>
 *  <p>Company:wjggy  </p>
 *  @author xiaofan.yu  
 *  @date   2012-2-3 下午05:47:51    
 *  @project  wjggy
 *  @version  1.0.0
 *  @see   
 *  @since 1.6 
 */
@Controller
@RequestMapping("/admin/category")
public class CategoryAction {
	@Resource(name="categoryService")
	CategoryService categoryService;
	
	/**
	 * @describe 板块列表
	 * @return
	 */
	@RequestMapping(value="",method=RequestMethod.GET)
	public ModelAndView categoryList() {
		ModelAndView model=ViewUtil.findView(PostsView.CATEGORY_URL);
		List<Category> list=categoryService.findCategory(null);
		model.addObject("categorys" , list);
		return model;
	}
	
	/**
	 * 
	 *  @describe 查找板块
	 *  @param request
	 *  @param response
	 *  @return
	 */
	@RequestMapping("/findCategory")
	public ModelAndView findCategory(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView model=ViewUtil.findView(PostsView.CATEGORY_URL);
		List<Category> list=categoryService.findCategory(null);
//		for (Category category :list) {
//			System.out.println(category.getIspublic());
//		}
		 model.addObject("categorys" , list);
		return  model;
	}
	public CategoryService getCategoryService() {
		return categoryService;
	}
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	@RequestMapping(value="update/{id}",method=RequestMethod.GET)
	public ModelAndView updateCategory(@PathVariable Long id,HttpServletRequest request, HttpServletResponse response){
		ModelAndView model=ViewUtil.findView(PostsView.CATEGORY_UPDATE_URL);
		Category category=new Category();
		category.setCategoryid(id);
		Category cc=categoryService.getCategory(category.getCategoryid());
		model.addObject("category" ,cc);
		return  model;
	}
	@RequestMapping(value="update",method=RequestMethod.POST)
	public ModelAndView updateCategory(Category category,HttpServletRequest request, HttpServletResponse response){
		categoryService.updateCategory(category);
		return  categoryList();
	}
	@RequestMapping(value="add",method=RequestMethod.POST)
	public ModelAndView addCategory(Category category,HttpServletRequest request, HttpServletResponse response){
		categoryService.addCategory(category);
		return  categoryList();
	}
	@RequestMapping(value="add",method=RequestMethod.GET)
	public ModelAndView addCategory(HttpServletRequest request, HttpServletResponse response){
		ModelAndView model=ViewUtil.findView(PostsView.CATEGORY_ADD_URL);
		return  model;
	}
	
	@RequestMapping(value="delete/{id}",method=RequestMethod.GET)
	public ModelAndView deleteCategory(@PathVariable Long id,HttpServletRequest request, HttpServletResponse response){
		Category category=new Category();
		category.setCategoryid(id);
		categoryService.deleteCategory(category);
		return  categoryList();
	}
}

